<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::Reflection::ClassMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::Reflection::ClassMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activerecord/lib/active_record/reflection_rb.html">
                vendor/rails/activerecord/lib/active_record/reflection.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Reflection allows you to interrogate Active Record classes and objects
about their associations and aggregations. This information can, for
example, be used in a form builder that took an Active Record object and
created input fields for all of the attributes depending on their type and
displayed the associations to other objects.
</p>
<p>
You can find the interface for the AggregateReflection and
AssociationReflection classes in the abstract <a
href="MacroReflection.html">MacroReflection</a> class.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001313">create_reflection</a>&nbsp;&nbsp;
      <a href="#M001316">reflect_on_aggregation</a>&nbsp;&nbsp;
      <a href="#M001315">reflect_on_all_aggregations</a>&nbsp;&nbsp;
      <a href="#M001317">reflect_on_all_associations</a>&nbsp;&nbsp;
      <a href="#M001318">reflect_on_association</a>&nbsp;&nbsp;
      <a href="#M001314">reflections</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001313" class="method-detail">
        <a name="M001313"></a>

        <div class="method-heading">
          <a href="#M001313" class="method-signature">
          <span class="method-name">create_reflection</span><span class="method-args">(macro, name, options, active_record)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001313-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001313-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/reflection.rb, line 13</span>
13:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create_reflection</span>(<span class="ruby-identifier">macro</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">active_record</span>)
14:         <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">macro</span>
15:           <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:has_many</span>, <span class="ruby-identifier">:belongs_to</span>, <span class="ruby-identifier">:has_one</span>, <span class="ruby-identifier">:has_and_belongs_to_many</span>
16:             <span class="ruby-identifier">klass</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:through</span>] <span class="ruby-operator">?</span> <span class="ruby-constant">ThroughReflection</span> <span class="ruby-operator">:</span> <span class="ruby-constant">AssociationReflection</span>
17:             <span class="ruby-identifier">reflection</span> = <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">macro</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">active_record</span>)
18:           <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:composed_of</span>
19:             <span class="ruby-identifier">reflection</span> = <span class="ruby-constant">AggregateReflection</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">macro</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">active_record</span>)
20:         <span class="ruby-keyword kw">end</span>
21:         <span class="ruby-identifier">write_inheritable_hash</span> <span class="ruby-identifier">:reflections</span>, <span class="ruby-identifier">name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">reflection</span>
22:         <span class="ruby-identifier">reflection</span>
23:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001316" class="method-detail">
        <a name="M001316"></a>

        <div class="method-heading">
          <a href="#M001316" class="method-signature">
          <span class="method-name">reflect_on_aggregation</span><span class="method-args">(aggregation)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the AggregateReflection object for the named <tt>aggregation</tt>
(use the symbol). Example:
</p>
<pre>
  Account.reflect_on_aggregation(:balance) # returns the balance AggregateReflection
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001316-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001316-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/reflection.rb, line 44</span>
44:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">reflect_on_aggregation</span>(<span class="ruby-identifier">aggregation</span>)
45:         <span class="ruby-identifier">reflections</span>[<span class="ruby-identifier">aggregation</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">AggregateReflection</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">reflections</span>[<span class="ruby-identifier">aggregation</span>] <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>
46:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001315" class="method-detail">
        <a name="M001315"></a>

        <div class="method-heading">
          <a href="#M001315" class="method-signature">
          <span class="method-name">reflect_on_all_aggregations</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns an array of AggregateReflection objects for all the aggregations in
the class.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001315-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001315-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/reflection.rb, line 36</span>
36:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">reflect_on_all_aggregations</span>
37:         <span class="ruby-identifier">reflections</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">select</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">reflection</span><span class="ruby-operator">|</span> <span class="ruby-identifier">reflection</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">AggregateReflection</span>) }
38:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001317" class="method-detail">
        <a name="M001317"></a>

        <div class="method-heading">
          <a href="#M001317" class="method-signature">
          <span class="method-name">reflect_on_all_associations</span><span class="method-args">(macro = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns an array of AssociationReflection objects for all the associations
in the class. If you only want to reflect on a certain association type,
pass in the symbol (<tt>:has_many</tt>, <tt>:has_one</tt>,
<tt>:belongs_to</tt>) for that as the first parameter. Example:
</p>
<pre>
  Account.reflect_on_all_associations             # returns an array of all associations
  Account.reflect_on_all_associations(:has_many)  # returns an array of all has_many associations
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001317-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001317-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/reflection.rb, line 55</span>
55:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">reflect_on_all_associations</span>(<span class="ruby-identifier">macro</span> = <span class="ruby-keyword kw">nil</span>)
56:         <span class="ruby-identifier">association_reflections</span> = <span class="ruby-identifier">reflections</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">select</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">reflection</span><span class="ruby-operator">|</span> <span class="ruby-identifier">reflection</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">AssociationReflection</span>) }
57:         <span class="ruby-identifier">macro</span> <span class="ruby-value">? </span><span class="ruby-identifier">association_reflections</span>.<span class="ruby-identifier">select</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">reflection</span><span class="ruby-operator">|</span> <span class="ruby-identifier">reflection</span>.<span class="ruby-identifier">macro</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">macro</span> } <span class="ruby-operator">:</span> <span class="ruby-identifier">association_reflections</span>
58:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001318" class="method-detail">
        <a name="M001318"></a>

        <div class="method-heading">
          <a href="#M001318" class="method-signature">
          <span class="method-name">reflect_on_association</span><span class="method-args">(association)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the AssociationReflection object for the named <tt>association</tt>
(use the symbol). Example:
</p>
<pre>
  Account.reflect_on_association(:owner) # returns the owner AssociationReflection
  Invoice.reflect_on_association(:line_items).macro  # returns :has_many
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001318-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001318-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/reflection.rb, line 65</span>
65:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">reflect_on_association</span>(<span class="ruby-identifier">association</span>)
66:         <span class="ruby-identifier">reflections</span>[<span class="ruby-identifier">association</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">AssociationReflection</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">reflections</span>[<span class="ruby-identifier">association</span>] <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>
67:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001314" class="method-detail">
        <a name="M001314"></a>

        <div class="method-heading">
          <a href="#M001314" class="method-signature">
          <span class="method-name">reflections</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a hash containing all AssociationReflection objects for the current
class Example:
</p>
<pre>
  Invoice.reflections
  Account.reflections
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001314-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001314-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/reflection.rb, line 31</span>
31:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">reflections</span>
32:         <span class="ruby-identifier">read_inheritable_attribute</span>(<span class="ruby-identifier">:reflections</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">write_inheritable_attribute</span>(<span class="ruby-identifier">:reflections</span>, {})
33:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>